"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '04/07/2022'
    comment: 自定义异常
"""

# 异常code定义
DEFAULT_ERROR_CODES = {
    400: "请求参数/格式错误",
    401: "当前用户没有登陆",
    402: "已在其他平台登陆",
    403: "没有操作权限",
    404: "资源不存在",
    405: "不允许调用该方法",
    406: "用户已冻结",
    408: "请求超时",
    409: "用户信息解析失败",
    410: "需要传入版本号",
    411: "有新版本",
    412: "用户登录已失效",
    413: "用户信息格式有误",
    500: "服务器异常",
    501: "暂不支持此接口",
    600: "自定义错误",
    700: "通过微信登陆授权的时候，提示用户未绑定手机号",
}


class BizError(Exception):
    """通用逻辑错误"""

    def __init__(self, message="", code=600, data=None):
        super().__init__()
        self.code = code

        msg_head = DEFAULT_ERROR_CODES.get(code)
        if message:
            # 有新提示, 覆盖原来的
            self.message = message
        elif msg_head:
            self.message = msg_head
        else:
            self.message = "未知错误"
        self.data = data

    def __str__(self):
        return self.message


class NotPathError(Exception):
    """非有效路径异常"""

    def __init__(self, path: str):
        super().__init__()
        self.path = path

    def __str__(self) -> str:
        return f"非有效路径-{self.path}"
